﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace InsideDL.MISControl
{
    public partial class frmMatrix : Form
    {
        
        protected DateTime newDateTime
        {
            get
            {
                return dtPick.Value;
            }
        }
        protected string newFile = string.Empty;
        protected string ftp = string.Empty;
        protected string id = string.Empty;
        protected string pw = string.Empty;
        protected string currentYM = string.Empty;
        protected string[] downList = null;
        //protected EventHandler ExecuteClick ;
        protected List<ucSelFileOrPath> controlList = null;

        public frmMatrix()
        {
            InitializeComponent();
        }

        protected void closeForm()
        {
            this.Close();
        }

     
        protected virtual void showMessage(string p_title, string p_msg)
        {
            MessageBox.Show(p_msg, p_title);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_name">要找的property 名稱</param>
        /// <param name="p_value">要從config中的欄位名</param>
        //protected virtual void setProperty(string[] p_name,string[] p_value)
        //{
        //    System.Reflection.PropertyInfo pi = this.GetType().GetProperty("");
        //    pi.SetValue(this, p_value, null);
        //}

        protected void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                //MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
                showMessage("ERROR", "Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        protected void showStatus(string p_msg,Label p_lbl)
        {
            p_lbl.Text = p_msg;
            p_lbl.Refresh();
            Application.DoEvents();
        }

        protected void execute(string p_p)
        {
            try
            {
                execute();
            }
            catch (Exception ex)
            {
                showMessage("ERROR", ex.Message);
            }
        }

        protected virtual void execute()
        { }

        protected virtual void ucClickFacade(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            switch (btn.Text)
            {
                case "離開":
                    this.closeForm();
                    break;
                case "執行":
                    execute(string.Empty);
                    break;
                default:
                    //if (ExecuteClick != null)
                    //{ ExecuteClick(sender, e); }
                    break;
            };
        }

        protected virtual bool Vaildation()
        {
            object reObj = null;
            foreach (UserControl uc in controlList)
            {
                reObj = Utility.MethodInvoke(uc, "Verify", null);
                if (Convert.ToBoolean(reObj).Equals(true))
                {
                    Utility.MethodInvoke(uc, "SetFocus", null);                    
                    return false;
                }
            }
            return true;
        }

        protected virtual void createNewFile(string p_fleName,string p_path
            ,string p_sourceFile)
        {
            //也許可以用Path.Combine
            newFile = string.Format(@"{1}\{0}.xls"
                , p_fleName
                , p_path
                );
            File.Copy(p_sourceFile, newFile, true);
        }

        private void frmMatrix_DoubleClick(object sender, EventArgs e)
        {
            dtPick.Visible = dtPick.Visible.Equals(false) ? true : false;
            if(dtPick.Visible.Equals(true))
            {
                dtPick.Location = new Point(10, 10);                
            }
        }

        /// <summary>
        /// 生出可以執行的BAT檔
        /// </summary>
        /// <param name="p_bat">bat file anme</param>
        /// <param name="p_fileName"></param>
        protected void createSQLBat(string p_bat,string p_fileName)
        {
            string str, tmp;
            str = string.Format(@"
OSQL -S (local)\SQLEXPRESS -E -i {0}\{1}.sql
PAUSE
", Program.TmpPath(),p_fileName);
            tmp = string.Format("{0}\\{1}", Program.TmpPath(), p_bat);
            Program.CreateFile(tmp, str);
        }

        protected void executeBat(string p_tmp)       
        {
            System.Diagnostics.ProcessStartInfo p = 
                new System.Diagnostics.ProcessStartInfo(p_tmp);
            p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            System.Diagnostics.Process process =
                System.Diagnostics.Process.Start(p);
            process.WaitForExit();
            if (!process.HasExited)
            {
                process.Kill();
            }
            showMessage(this.Text, "完成！");
        }
    }
}
